using System;
using System.Data;
using System.Configuration;
using Pheonix.Fashionzoomer.DAL;
using System.Data.SqlClient;
using System.Collections;
using Pheonix.CodeLibrary.Data;
using Pheonix.CodeLibrary;

/// <summary>
/// Summary description for Advertise
/// </summary>

namespace Pheonix.Fashionzoomer.Entity.SqlServer2000
{
    [System.ComponentModel.DataObject]
    public class Advertise : SqlServerDAO
    {
        #region Advertise Private Fields

		private Int32 _id;
		private String _title;
		private String _imageFolder;
		private String _companyName;

        #endregion

        #region Advertise Constractors

		public Advertise(){}

		public Advertise
		(
			Int32 id,
			String title,
			String imageFolder,
			String companyName

		)
		{
			this._id = id;
			this._title = title;
			this._imageFolder = imageFolder;
			this._companyName = companyName;
		}

        #endregion

        #region Advertise Properties

		public Int32 Id
		{
			get{ return this._id; }
			set{ this._id = value; }
		}

		public String Title
		{
			get{ return this._title; }
			set{ this._title = value; }
		}

		public String ImageFolder
		{
			get{ return this._imageFolder; }
			set{ this._imageFolder = value; }
		}

		public String CompanyName
		{
			get{ return this._companyName; }
			set{ this._companyName = value; }
		}


        #endregion

        #region Advertise Static Methods

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Insert, true)]
        public static Int32 CreateNewAdvertise
        (
			String title,
			String imageFolder,
			String companyName

        )
        {
            //creating an empty Advertise object
            Advertise advertise = new Advertise();
            advertise.Title = title;
			advertise.ImageFolder = imageFolder;
			advertise.CompanyName = companyName;

            return Advertise.CreateNewAdvertise(advertise);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool UpdateAdvertise
        (
			Int32 id,
			String title,
			String imageFolder,
			String companyName

        )
        {
            Advertise advertise = Advertise.GetAdvertiseById(id);
            advertise.Title = title;
			advertise.ImageFolder = imageFolder;
			advertise.CompanyName = companyName;

            return Advertise.UpdateAdvertise(advertise);
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Update, true)]
        public static bool DeleteAdvertise
        (
            Int32 id
        )
        {
            //Validate Input
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Advertise"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_ADVERTISE_DELETEADVERTISE);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);

        }

        public static bool DeleteAdvertiseList(System.Collections.Generic.List<Int32> idListToDelete)
        {
            //Validate Input
            foreach (Int32 id in idListToDelete)
                if (id <= GetIdMinValue)
                    throw (new ArgumentOutOfRangeException("id in Advertise"));

            //performing deletion operation //

            string csvString = Utility.MakeCSV(idListToDelete);

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            AddParameterToSqlCmd(sqlCmd, "@IdList", SqlDbType.NVarChar, csvString.Length, 
                ParameterDirection.Input, csvString);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_ADVERTISE_DELETEADVERTISELIST);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == idListToDelete.Count ? true : false);
        }
		
        #endregion

        #region Advertise Instance Methods
        
        public bool Save()
		{
			if(Id <= GetIdMinValue)
			{
				Int32 TempId=CreateNewAdvertise(this);
				if(TempId>0)
				{
					_id=TempId;
					return true;
				}
				else
				{
					// error occurs
					return false;
				}
			}
			else
			{	//This is a update Command
				return (UpdateAdvertise(this));
			}
		}

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Advertise> GetAllAdvertise()
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_ADVERTISE_GETALLADVERTISE);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateAdvertiseCollectionFromReader);
            CustomCollection<Advertise> objCollection = ((CustomCollection<Advertise>)GetAllData(sqlCmd, result));

            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static CustomCollection<Advertise> GetAllAdvertisePaged(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            //Validate Input
            if (orderBy == "")
                orderBy = "Id";

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@orderby", SqlDbType.VarChar, 50, ParameterDirection.Input, orderBy);
            AddParameterToSqlCmd(sqlCmd, "@startRowIndex", SqlDbType.Int, 0, ParameterDirection.Input, startRowIndex);
            AddParameterToSqlCmd(sqlCmd, "@maximumRows", SqlDbType.Int, 0, ParameterDirection.Input, maximumRows);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_ADVERTISE_GETALLADVERTISEPAGED);

            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateAdvertiseCollectionFromReader);
            CustomCollection<Advertise> objCollection = ((CustomCollection<Advertise>)GetAllData(sqlCmd, result));
            return objCollection;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static int GetAllAdvertisePagedCount(String orderBy, Int32 startRowIndex, Int32 maximumRows)
        {
            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();

            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_ADVERTISE_GETALLADVERTISEPAGEDCOUNT);
            GetOneData(sqlCmd);

            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return returnValue;
        }

        [System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
        public static Advertise GetAdvertiseById(Int32 id)
        {
            if (id <= GetIdMinValue)
                throw (new ArgumentOutOfRangeException("id in Advertise"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, id);
            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_ADVERTISE_GETADVERTISEBYID);
            GenerateCollectionFromReader result = new GenerateCollectionFromReader(GenerateAdvertiseCollectionFromReader);
            CustomCollection<Advertise> objCollection = ((CustomCollection<Advertise>)GetAllData(sqlCmd, result));

            if (objCollection.Count > 0)
                return (Advertise)objCollection[0];
            else
                return null;
        }

        public static Int32 CreateNewAdvertise(Advertise newAdvertise)
        {
            // validate Parameters
            if (newAdvertise == null)
                throw (new ArgumentNullException("newAdvertise in Advertise"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Title", SqlDbType.NVarChar, 128, ParameterDirection.Input, newAdvertise.Title);
			AddParameterToSqlCmd(sqlCmd, "@ImageFolder", SqlDbType.NVarChar, 128, ParameterDirection.Input, newAdvertise.ImageFolder);
			AddParameterToSqlCmd(sqlCmd, "@CompanyName", SqlDbType.NVarChar, 128, ParameterDirection.Input, newAdvertise.CompanyName);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_ADVERTISE_CREATENEWADVERTISE);
            GetOneData(sqlCmd);
            return ((Int32)sqlCmd.Parameters["@ReturnValue"].Value);

        }

        public static bool UpdateAdvertise(Advertise newAdvertise)
        {
            // validate Parameters
            if (newAdvertise == null)
                throw (new ArgumentNullException("newAdvertise"));

            // Execute SQL Command
            SqlCommand sqlCmd = new SqlCommand();
            AddParameterToSqlCmd(sqlCmd, "@ReturnValue", SqlDbType.Int, 0, ParameterDirection.ReturnValue, null);

            AddParameterToSqlCmd(sqlCmd, "@Id", SqlDbType.Int, 0, ParameterDirection.Input, newAdvertise.Id);
            AddParameterToSqlCmd(sqlCmd, "@Title", SqlDbType.NVarChar, 128, ParameterDirection.Input, newAdvertise.Title);
			AddParameterToSqlCmd(sqlCmd, "@ImageFolder", SqlDbType.NVarChar, 128, ParameterDirection.Input, newAdvertise.ImageFolder);
			AddParameterToSqlCmd(sqlCmd, "@CompanyName", SqlDbType.NVarChar, 128, ParameterDirection.Input, newAdvertise.CompanyName);

            SetCommandType(sqlCmd, CommandType.StoredProcedure, SPR_ADVERTISE_UPDATEADVERTISE);
            GetOneData(sqlCmd);
            int returnValue = (int)sqlCmd.Parameters["@ReturnValue"].Value;
            return (returnValue == 0 ? false : true);
        }

        #endregion

        #region Advertise Related Collections
        /// <summary>
        /// Creates and returns a strongly typed collection of Advertise custom entity. 
        /// The colelction is created through iterating on the IdataReader object which contains 
        /// Advertise information, as a set of records, similar to tabular format.
        /// </summary>
        public static IList GenerateAdvertiseCollectionFromReader(IDataReader returnData)
        {

            //creating the instance of Advertise collection
            CustomCollection<Advertise> colAdvertise = new CustomCollection<Advertise>();

            //Iterating thriugh the data reader, to generate Advertise collection.

            //getting the Advertise collection by iterating thru the data reader
            while (returnData.Read())
            {
                //passing the Advertise constructor parameters from the current instance of data reader fields.
                Advertise newAdvertise = new Advertise
                (
                    returnData["id"] == System.DBNull.Value ? GetIdMinValue : (Int32)returnData["id"],
                    returnData["title"] == System.DBNull.Value ? String.Empty : (String)returnData["title"],
					returnData["image_folder"] == System.DBNull.Value ? String.Empty : (String)returnData["image_folder"],
					returnData["company_name"] == System.DBNull.Value ? String.Empty : (String)returnData["company_name"]

                );
                //adding the Advertise to the collection
                colAdvertise.Add(newAdvertise);
            }

            //returns the collection of Advertise 
            return (colAdvertise);
        }

        #endregion

        #region Constants and Default Values

        public static Int32 GetIdMinValue
        {
            get
            {
                return 0;
            }
        }

        //contants
        internal const string SPR_ADVERTISE_CREATENEWADVERTISE = "spr_Advertise_CreateNewAdvertise";
        internal const string SPR_ADVERTISE_UPDATEADVERTISE = "spr_Advertise_UpdateAdvertise";
        internal const string SPR_ADVERTISE_DELETEADVERTISE = "spr_Advertise_DeleteAdvertise";
        internal const string SPR_ADVERTISE_DELETEADVERTISELIST = "spr_Advertise_DeleteAdvertiseList";
        internal const string SPR_ADVERTISE_GETADVERTISEBYID = "spr_Advertise_GetAdvertiseById";
        internal const string SPR_ADVERTISE_GETALLADVERTISE = "spr_Advertise_GetAllAdvertise";
        internal const string SPR_ADVERTISE_GETALLADVERTISEPAGED = "spr_Advertise_GetAllAdvertisePaged";
        internal const string SPR_ADVERTISE_GETALLADVERTISEPAGEDCOUNT = "spr_Advertise_GetAllAdvertisePagedCount";

		//Foreign key contants
		
        #endregion
    }
}
